#include "all.h"

GameRankingHandler::GameRankingHandler( )
{
	playtime =  MAX_TIME;
	string fileName = "";
	char dir[128];
	_getcwd(dir, 128);
	fileName.append( dir );
	fileName.append( resultPath );
	rankingfilename = fileName;
	init();

}

GameRankingHandler::~GameRankingHandler()
{
// 	fs.flush();
// 	fs.close();
}

void GameRankingHandler::writeResult( string name, int result )
{
	int pos = getPos( result );
	if ( pos == FAIL )
	{
		return ;
	}
	for ( int i = MAX_ROLE_LEN - 1 ; i > pos; i-- )
	{
		memcpy( players[i].name, players[i-1].name, MAX_ROLE_LEN);
		players[i].result = players[i-1].result;
	}
	memcpy(players[pos].name, name.c_str(), MAX_ROLE_LEN );

	players[pos].result = result;
	FILE* rankingfile ;
	if ( ( rankingfile = fopen( rankingfilename.c_str() ,"wb+")) == NULL )
	{
		//open ERROR
		return ;
	}
	for ( int i = 0 ; i < MAX_ROLE_LEN ; i++ )
	{
		fwrite( (void*)(&players[i]), sizeof(RoleData), 1, rankingfile );
	}
	
	//fputs( record, rankingfile );
	fclose( rankingfile );

}

void GameRankingHandler::writeResult( string name, int result , int pos )
{

}

int GameRankingHandler::readResult( string& name, int& result, int roleIndex )
{
	return 0;
}

int GameRankingHandler::string_to_int( string s )
{
	istringstream instr(s);
	int x ;
	instr>>x;
	return x;
}
int GameRankingHandler::getPos( int result )
{
	for ( int i = 0 ; i < MAX_ROLE_LEN ; ++i )
	{
		if ( players[i].result > result )
		{
			return i;
		}
	}
	return FAIL ;
}
void GameRankingHandler::init()
{
	for ( int i = 0 ; i < MAX_ROLE_LEN ; i++ )
	{
		//players[i].name = "NONE";
		memcpy( players[i].name, "NONE",5);
		players[i].result = DEFAULT_RESULT ;
	}
	//------------------
	FILE* rankingfile;
	char record[ MAX_ROLE_LEN * RECORD_LEN ];
	memset( record, 0, MAX_ROLE_LEN * RECORD_LEN );
	if ( ( rankingfile = fopen( rankingfilename.c_str(),"rb+")) == NULL)
	{
		if ( (rankingfile = fopen( rankingfilename.c_str(),"wb+")) == NULL)
		{
			return ;
		}
	}
	for ( int i = 0 ; i < MAX_ROLE_LEN; i++ )
	{
		fread( &players[i], sizeof(RoleData), 1, rankingfile );
	}
	fclose(rankingfile);
}

string GameRankingHandler::getFormatName(int index)
{
	string name = players[index].name;
	int len = name.length() ;
	if ( DEFAULT_FORMAT == FORMAT_TYPE_CENTER )
	{
		//todo
	}
	else if ( DEFAULT_FORMAT == FORMAT_TYPE_LEFT)
	{
		while( len < ROLE_NAME_LEN )
		{
			name.append( PLACE_HOLDER );
			len++; 
		}
	}
	else
	{
		//error ,left
		while( len < ROLE_NAME_LEN )
		{
			name.append( PLACE_HOLDER );
			len++; 
		}
	}
	return name;
}



string GameRankingHandler::getFormateResult(int index)
{
	string str = "";
	int result = players[index].result;
	if ( result > DEFAULT_RESULT )
	{
		result = DEFAULT_RESULT;
	}
	int minSeconds = result % 100;
	int seconds = (result/100)%60;
	int minites = ( result/100/60 )%60 ;


	str = str + intToStr( minites ) + ":" + intToStr( seconds ) + ":" + intToStr( minSeconds );
	return str ;
}

string GameRankingHandler::intToStr(int num)
{
	char str[3] = { 0 };

	str[1] = intchar[ num % 10 ] ;
	str[0] = intchar[ num / 10 ] ;

	return string( str );
}

int GameRankingHandler::getRandomResult()
{
	return rand() %( DEFAULT_RESULT + 1);
}

int GameRankingHandler::calgametime()
{
	
	endtime = clock();
	if ( endtime - begintime > 5000 )
	{
		playtime = (endtime - begintime - 5000)/10;
	}

	return playtime;
}

int GameRankingHandler::getplaytime()
{
	return playtime ;
}
int GameRankingHandler::startgame()
{
	begintime = clock();
	return 0;
}

bool GameRankingHandler::intheranking()
{
	int t = calgametime();
	int ranking  = getPos( t );
	if ( ranking == FAIL)
	{
		return false;
	}
	return true;
}
